/*
*作者---杨彬
*
*/
<template>
    <div>
    <header>
      <span class="title-text" v-text="sData.goPay.orderMoneyText"></span>
      <span class="price-warp com-div-middle-ab">
        <span class="symbol" v-text="sData.productCarSData.moneySymbol"></span>
        <span class="val" v-text="amount"></span>
      </span>
    </header>
    <section>
      <div class="div-pay"></div>
      <div class="item-pay" v-for="gitem in sData.goPay.payWay">
        <label class="com-div-middle-ab">
          <input type="radio" name="payWay" v-model='payWays' value="payWays">
          <img :src="hitem" v-for="hitem in sData.img.product">
        </label>
        <img :src="gitem.src">
        <span v-text="gitem.name"></span>
      </div>
    </section>
    <footer>
      <button class="button" type="button" v-text="sData.goPay.submitText" @click="surePay" v-if='payWays'></button>
      <button class="button none-button" type="button" v-text="sData.goPay.submitText" v-if='!payWays'></button>
    </footer>
    </div>
</template>
<script type="text/javascript">
export default {
  props:['sData'],
  data(){
    return{
      payWays:'',
      amount:''
    }
  },
  created () {
    this.postAjax(this.sData.url.getPaidAmountByInfoUrl,{ userId : this.sData.userId,orderId : this.$route.params.orderId},(res)=>{
      //取消成功再次获取数据
      if(res && res.status==200){
        this.amount=res.data;
      }else {
        m$.template({
          val:res.message,
          time:1500
        })
      }
    })
  },
  methods:{
    surePay(){
      this.postAjax(this.sData.url.payWeixin,{ orderId : this.$route.params.orderId,amount : this.amount},(res)=>{
        //取消成功再次获取数据
        if(res && res.message == 'OK' ){
          var othis = this;
          function onBridgeReady(){
            WeixinJSBridge.invoke(
               'getBrandWCPayRequest', {
                  "appId":res.data.appId,     //公众号名称，由商户传入     
                  "timeStamp":res.data.timeStamp,         //时间戳，自1970年以来的秒数     
                  "nonceStr":res.data.nonceStr, //随机串     
                  "package":res.data.package,     
                  "signType":"MD5",         //微信签名方式：     
                  "paySign":res.data.paySign //微信签名 
                },
                function(res){   
                  if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    m$.template({
                      val: '支付成功',
                      time: 1500
                    })// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
                    othis.$router.replace('/orderList/-1');
                  }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                    m$.template({
                      val: '取消支付',
                      time: 1500
                    })
                  }
              }
            ); 
          }
          if (typeof WeixinJSBridge == "undefined"){
             if( document.addEventListener ){
                 document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
             }else if (document.attachEvent){
                 document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
                 document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
             }
          }else{
             onBridgeReady();
          }
          //this.$router.replace('/orderList/-1');
        }else {
          m$.template({
            val: '支付失败,请重试',
            time: 1500
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
    @import "goPay";
</style>
